# -*- coding:UTF-8 -*-

from django.db.models import Q
from cmdb.models import ConfigurationInfo
from cmdb.models import CategoryInfo

import datetime

def get_biz_type_info():

	second_biz_type = CategoryInfo.objects.filter(grade=2)
	return second_biz_type

def add_configuration_meta_info(svn_path, manage_path, category, cursor):
	current_time = str(datetime.datetime.now()).split('.')[0]
	add_cfg_info_sql = "insert into configuration_info set svn_path='" \
		+ svn_path + "', manage_path='" +manage_path+ "', category=" \
		+category+", createtime='" + current_time + "'"
	cursor.execute(add_cfg_info_sql)

def list_configuration_meta(start, end):
	count = ConfigurationInfo.objects.count()
	config_metas = ConfigurationInfo.objects.order_by('-id')[start:end]
	return {'count' : count , 'config_meta_list' : config_metas}

def update_configuration_meta(svn_path, manage_path, category, id, cursor):
	update_sql = "update configuration_info set svn_path='" + svn_path \
					+ "', manage_path='"+ manage_path +"', category="\
					+ category + " where id=" + id
	cursor.execute(update_sql)

def delete_cfg_meta(id):
	ConfigurationInfo.objects.get(id=id).delete()

def un_biz_type_info(id):
	return CategoryInfo.objects.exclude(id=id).filter(grade=2)

def get_cfg_meta_info(id):

	try:
		cfg_info = {}
		# print '123...', id
		cfg_info = ConfigurationInfo.objects.get(id=id)
		category_id = int(cfg_info.category)
		# print "234",  category_id

		if category_id:
			# print CategoryInfo.objects.get(1).__dict__
			category_obj = CategoryInfo.objects.get(id=category_id)
			cfg_info.category_name = category_obj.category_name
			categorys = un_biz_type_info(category_id)
			return (cfg_info, categorys)

	except Exception, e:
		print 'get_cfg_meta_info: ' , e
